import { proxy } from "valtio"
import { get, post, put, del } from "../request"
import { io } from 'socket.io-client';
import { AnyARecord } from "dns";
interface TypeDocument {
    _id: string;
    title: string;
    prent?: string;
    children?: TypeDocument[];
    text: string
}
interface ProductDocument {
    shopname: string;
    StoreCode: String;
    types: string;
    entity: string;
    price: number;
    vipprice: number;
    takeout: number;
    boxfee: number;
    image?: string;
    text: string;
    state: boolean;
}
interface Max {
    name?: string;
    shopname?: string;
    price?: number;
    vipprice?: number;
    cost?: number;
    boxfee?: number;
    image?: string;
    text?: string;
    state?: boolean;
    children?: Array<any>;
}
const socket = io('http://localhost:3000')
export const order = proxy({
    treedata: [] as TypeDocument[],
    treedatas: [] as TypeDocument[],
    data: [] as ProductDocument[],
    max: [] as Max[],
    dateedit: {
        _id: "",
        name: "",
        shopcode: "",
        shopname: "",
        types: [],
        entity: "",
        price: 0,
        vipprice: 0,
        cost: 0,
        text: "",
        state: false
    },
    typs: {
        _id: "",
        title: "",
        prent: "",
        text: "",
        parenttitle: ""
    },
    getprolist: () => {
        get("/produce").then(res => {
            order.data = res
        })
    },
    editpro: (item) => {
        put("/produce/editpro", item).then(res => {
            console.log(res);
        })
    },
    maxshow: {
        _id: "",
        name: "",
        shopname: "",
        price: "",
        vipprice: "",
        cost: "",
        boxfee: "",
        image: "",
        text: "",
        state: null,
        children: [],
    },
    getTypes: async () => {
        const res = await get("/Types")
        order.treedata = res.data
    },
    addType: ({ title, prent, text, label }) => {
        post("/Types/add", { title: title, prent: prent, text: text, parenttitle: label }).then(res => {
            order.treedata = res.data
        })
    },
    showdata: (item: any) => {
        order.maxshow = { ...item }
    },
    delone: (id) => {
        del("/produce/delone", { _id: id }).then(res => {
            console.log(res);
            order.getprolist()
        })
    },

    maxshows: (item: any) => {
        order.dateedit = { ...item }
    },
    getmax: () => {
        get("/Max").then(res => {
            order.max = res
        })
    },
    addproduce: (name, shopcode, shopname, types, entity, price, vipprice, cost, text, state, boxfee) => {
        post("/produce/addpro", { name: name, shopcode: shopcode, shopname: shopname, boxfee: boxfee, types: types, entity: entity, price: price, vipprice: vipprice, cost: cost, text: text, state: state }).then(res => {
            console.log(res)
        })
    },
    sendUpdate: (data: string) => {
        socket.emit('message', data);
    },
    initSocket: () => {
        socket.on('message', (data: any) => {
            // 更新组件状态或 UI
            order.getprolist()
        });
        return () => {
            socket.off('message');
        };
    },
    addmax: (name:any, shopname:any, boxfee:any, children:any, state:any) => {
        const totalPrice = children.reduce((sum:any, child:AnyARecord) => sum + parseFloat(child.price) * child.num, 0);
        const totalVipPrice = children.reduce((sum:any, child:any) => sum + parseFloat(child.price) * child.num * 0.86, 0);
        const totalCost = children.reduce((sum:any, child:any,) => sum + parseFloat(child.price) * child.num + 2, 0);
        post("/Max/add", {
            name: name,
            shopname: shopname,
            cost: totalCost,
            boxfee: boxfee,
            children: children,
            price: totalPrice,
            vipprice: totalVipPrice,
            state: state
        }).then(res => {
            console.log(res);
        });
    },
    getsmax: (nam:anye) => {
        post("/max/gets", { name: name }).then(res => {
            order.max = res.data
        })
    },
    delmax: (id:any) => {
        del("/Max/del", { _id: id }).then(res => {
            console.log(res);
            order.getmax()
        })
    },
    getType: (titles:any) => {
        post("/Types/get", { title: titles }).then(res => {
            order.treedata = res.data
        })
    },
    puttype: (item:any) => {
        order.typs = { ...item }
    },
    updateType: (title) => {
        put("/Types/update", { _id: order.typs._id, ...title }).then(res => {
            console.log(res);
            order.getTypes()
        })
    },
    editmax: (name, shopname, cost, boxfee, children, price, vipprice, state, chiname, entity, cprice, num) => {
        const totalPrice = children.reduce((sum, child) => sum + parseFloat(child.price) * child.num, 0);
        const totalVipPrice = children.reduce((sum, child) => sum + parseFloat(child.price) * child.num * 0.86, 0);
        const totalCost = children.reduce((sum, child) => sum + parseFloat(child.price) * child.num + 2, 0);
        put("/Max/editmax", {
            _id: order.maxshow._id,
            name: name,
            shopname: shopname,
            cost: totalCost,
            boxfee: boxfee,
            children: children,
            price: totalPrice,
            vipprice: totalVipPrice,
            state: state
        }).then(res => {
            console.log(res);
        });
    },
    search: (value, shopname, state, name) => {
        post("/produce/search", { name: name, shopname: shopname, state: state, value: value }).then(res => {
            order.data = res.data
        })
    },
    deletes: (item) => {
        del("/Types/delete", item).then(res => {
            console.log(res);

        })
        order.getTypes()
    }
})